<style scoped>
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="单据编号" prop="id">
        <el-input
          v-model="queryParams.linkedcode"
          placeholder="请输入出入库单编号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务单号" prop="kdcode">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入业务单编号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择单据类型" clearable size="mini">
          <el-option
            v-for="dict in typeOptions"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            :key="dict.dictValue"
             @click.native="getSubtype($event, dict)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="子类型" prop="subtype">
        <el-select v-model="queryParams.subtype" placeholder="请选择单据子类型" clearable size="mini">
          <el-option
            v-for="dict in subtypeOptions"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            :key="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库房" prop="srguid">
        <el-select v-model="queryParams.srguid" filterable placeholder="请选择库房" clearable size="mini">
          <el-option
            v-for="item in storerooms"
            :label="item.name"
            :value="item.guid"
            :key="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" filterable prop="supplierid">
        <el-select v-model="queryParams.supplierid" placeholder="请选择系统供应商" clearable size="mini">
          <el-option
            v-for="item in suppliers"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金蝶编号" prop="kdcode">
        <el-input
          v-model="queryParams.kdcode"
          placeholder="请输入金蝶业务编号"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in statusOptions"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务时间" prop="createtime">
        <el-date-picker
          v-model="setime"
          size="mini"
          type="daterange"
          unlink-panels
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="推送时间" prop="createtime">
        <el-date-picker
          v-model="ptime"
          size="mini"
          type="daterange"
          unlink-panels
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['copy:copytokd:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="copytokdList" @selection-change="handleSelectionChange">
      <el-table-column label="出入库单编号" align="center" prop="linkedcode" width="90">
        <template slot-scope="scope">
          <span @click="handleShow(scope.row)">{{ scope.row.linkedcode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务单编号" align="center" prop="code" >
      <template slot-scope="scope">
          <span @click="handleShow(scope.row)">{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金蝶单据类型" align="center" prop="kdtype" width="90" />
      <el-table-column label="类型" align="center" prop="type" :formatter="typeFormat" />
      <el-table-column label="业务日期" align="center" prop="orderdate" >
        <template slot-scope="scope">
          <span>{{formatDate1(scope.row.orderdate)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="库房" align="center" prop="srname" />
      <el-table-column label="库存组织" align="center" prop="orgname" width="100" />
      <el-table-column label="付款方式" align="center" prop="sellpaytype" :formatter="sellpaytypeFormat"/>
      <el-table-column label="金蝶付款方式" align="center" prop="kdsellpaytype" width="90" />
      <el-table-column label="数量" align="center" prop="totalqty" />
      <el-table-column label="币种" align="center" prop="currency" :formatter="currencyFormat" />
      <el-table-column label="币种编码" align="center" prop="currencyKdn" />
      <el-table-column label="供应商" align="center" prop="suppliername" width="120" />
      <el-table-column label="客户" align="center" prop="customername" width="120" />
      <el-table-column label="金蝶业务编号" align="center" prop="kdcode" width="90" />

      <el-table-column label="同步信息" align="center" prop="copyRemark" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="拉取时间" align="center" prop="createtime" />
      <el-table-column label="推送时间" align="center" prop="pushdate"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleShow(scope.row)"
          >详情</el-button>
          <el-button
            v-if="scope.row.status == 4 || scope.row.status == 5 || scope.row.status==8"
            size="mini"
            type="text"
            icon="el-icon-refresh-right"
            @click="handleStatus(scope.row)"
          >重新同步</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 明细数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-table v-loading="loading" :data="copytokditem">
        <el-table-column label="出入库单编号" align="center" prop="copytokd.linkedcode" v-if="show"/>
        <el-table-column label="业务单编号" align="center" prop="copytokd.code" />
        <el-table-column label="库房" align="center" prop="srname" />
        <el-table-column label="金蝶库房编码" align="center" prop="shrKdn" />
        <el-table-column label="库存组织" align="center" prop="orgname" />
        <el-table-column label="金蝶组织编码" align="center" prop="orgKdn" />
        <el-table-column label="产品" align="center" prop="productname" />
        <el-table-column label="物料" align="center" prop="spname" />
        <el-table-column label="金蝶物料编码" align="center" prop="materialKdn" />
        <el-table-column label="ERP数量" align="center" prop="qty" />
        <el-table-column label="ERP单位" align="center" prop="unit" />
        <el-table-column label="金蝶数量" align="center" prop="kdqty" />
        <el-table-column label="金蝶单位" align="center" prop="kingdeeunit" />
        <el-table-column label="是否赠送" align="center" prop="ispresent" :formatter="(row) => {return row.ispresent == 'true' ? `是` : `否`}" v-if="show"/>
        <el-table-column label="单价" align="center" prop="price" v-if="show"/>
        <el-table-column label="金额" align="center" prop="amount" v-if="show"/>
        <el-table-column label="供应商" align="center" prop="copytokd.suppliername" v-if="show"/>
        <el-table-column label="金蝶供应商编码" align="center" prop="balancesupplierKdn" v-if="show"/>
        <el-table-column label="客户" align="center" prop="copytokd.customername" v-if="show"/>
        <el-table-column label="成本中心" align="center" prop="departmentname" v-if="show016"/>
        <el-table-column label="成本中心金蝶编码" align="center" prop="departmentkdnum" v-if="show016"/>
        <el-table-column label="部门" align="center" prop="departmentname" v-if="show"/>
        <el-table-column label="部门金蝶编码" align="center" prop="departmentkdnum" v-if="show"/>
         <el-table-column label="成本对象名称" align="center" prop="cosname" v-if="show016"/>
        <el-table-column label="成本对象金蝶编码" align="center" prop="coskdnum" v-if="show016"/>
        <el-table-column label="金蝶客户编码" align="center" prop="ordercustomerKdn" v-if="show"/>
        <el-table-column label="金蝶更新类型" align="center" prop="kdupdatetype" v-if="show"/>
        <el-table-column label="同步信息" align="center" prop="copyRemark" />
        <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      </el-table>
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="cancel">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import { listCopytokd, getCopytokd, delCopytokd, addCopytokd, updateCopytokd, exportCopytokd, getCopytokditem, editCopytokd } from "@/api/copy/copytokd";
import { findStoreroom, findSupploerlist } from "@/api/public";

export default {
  name: "Copytokd",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待同步数据表格数据
      copytokdList: [],
      // 待同步数据明细数据
      copytokd: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 单据类型字典
      typeOptions: [],
      // 子类型字典
      subtypeOptions: [],
      // 付款方式字典
      sellpaytypeOptions: [],
      // 推送金蝶系统的单据类型006采购入库007	销售出库008 其他入库009其他出库字典
      kdtypeOptions: [],
      // 状态1-初始 2-待同步 3-资料不全，无法同步 4-同步失败 5-同步成功 6-已分拆字典
      statusOptions: [],
      currencyOptions: [],
      storerooms: [],
      suppliers: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        kdcode: null,
        type: null,
        subtype: null,
        kdtype: null,
        srguid: null,
        srKdn: null,
        orgguid: null,
        supplierid: null,
        customerid: null,
        status: null,
        createtime: null,
        beginTime: '',
        endTime: '',
        endPushDate: null,
        startPushDate: null
      },
      setime: '',
      ptime: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      copytokditem: [],
      show: true,
      show016: false
    };
  },
  created() {
    this.getList();
    this.getDicts("order_type").then(response => {
      this.typeOptions = response.data;
    });
    // this.getDicts("order_sub_type").then(response => {
    //   this.subtypeOptions = response.data;
    // });
    this.getDicts("kd_order_type").then(response => {
      this.kdtypeOptions = response.data;
    });
    this.getDicts("copy_data_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("currency_type").then(response => {
      this.currencyOptions = response.data;
    });
    this.getDicts("pay_type").then(response => {
      this.sellpaytypeOptions = response.data;
    });
    findStoreroom({}).then(response => {
      this.storerooms = response.data;
    });
    findSupploerlist({}).then(response => {
      this.suppliers = response.data;
    });
  },
  watch: {
    'setime': function(val) {
      this.queryParams.beginTime = val[0];
      this.queryParams.endTime = val[1];
    },
    'ptime': function(val) {
      this.queryParams.startPushDate = val[0];
      this.queryParams.endPushDate = val[1];
    }
  },
  methods: {
    getSubtype(event, item){
      if(item.dictValue == 1){
        //出
        this.getDicts("order_sub_type").then(response => {
          this.subtypeOptions = response.data;
          this.subtypeOptions = this.subtypeOptions.filter(item => item.remark == "出")
        });

      }else if(item.dictValue == 2){
        //入
        this.getDicts("order_sub_type").then(response => {
          this.subtypeOptions = response.data;
          this.subtypeOptions = this.subtypeOptions.filter(item => item.remark == "入")
        });
      }
    },
    /** 查询待同步数据列表 */
    getList() {
      this.loading = true;
      listCopytokd(this.queryParams).then(response => {
        this.copytokdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //日期格式化
    formatDate1(value,args){
      let dt=new Date(value)
      let year = dt.getFullYear();
      let month = (dt.getMonth() + 1).toString().padStart(2,'0');
      let date = dt.getDate().toString().padStart(2,'0');
      let hour = dt.getHours().toString().padStart(2,'0');
      let minute = dt.getMinutes().toString().padStart(2,'0');
      let second = dt.getSeconds().toString().padStart(2,'0');
      return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
    },
    // 单据类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 子类型字典翻译
    subtypeFormat(row, column) {
      return this.selectDictLabel(this.subtypeOptions, row.subtype);
    },
    // 推送金蝶系统的单据类型006采购入库007	销售出库008 其他入库009其他出库字典翻译
    kdtypeFormat(row, column) {
      return this.selectDictLabel(this.kdtypeOptions, row.kdtype);
    },
    // 状态1-初始 2-待同步 3-资料不全，无法同步 4-同步失败 5-同步成功 6-已分拆字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //币种
    currencyFormat(row, column) {
      return this.selectDictLabel(this.currencyOptions, row.currency);
    },
    // 付款方式字典翻译
    sellpaytypeFormat(row, column) {
      return this.selectDictLabel(this.sellpaytypeOptions, row.sellpaytype);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        linkedguid: null,
        orderguid: null,
        code: null,
        kdcode: null,
        type: null,
        subtype: null,
        kdtype: null,
        srguid: null,
        srname: null,
        srKdn: null,
        orgguid: null,
        orgKdn: null,
        orgname: null,
        totalqty: null,
        totalamount: null,
        supplierid: null,
        supplierKdn: null,
        suppliername: null,
        customerid: null,
        customerKdn: null,
        customername: null,
        status: 0,
        operator: null,
        operatorKdn: null,
        orderdate: null,
        copyTime: null,
        lastCopyTime: null,
        copyRemark: null,
        createtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待同步数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCopytokd(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改待同步数据";
      });
    },
    /** 详情操作按钮 */
    handleShow(row) {
      const id = row.id || this.ids
      const kdtype = row.kdtype;
      getCopytokditem({ctkdid : id}).then(response => {
        console.log(response)
        this.copytokditem = response.rows;
        this.open = true;
        this.title = "待同步数据订单明细";
      });
      if(kdtype == "016"){
        this.show = false;
        this.show016 = true;
      }else{
        this.show = true;
        this.show016 = false;
      }
      // getCopytokd(id).then(response => {
      //   this.copytokd = response.data;
      //   this.open = true;
      //   this.title = "待同步数据订单明细";
      // });
    },
    /** 重新同步操作按钮 */
    handleStatus(row) {
      const id = row.id || this.ids
      this.$confirm('确认执行该操作?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return editCopytokd({id : id});
      }).then(() => {
        this.getList();
        this.msgSuccess("同步成功");
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCopytokd(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCopytokd(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCopytokd(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有待同步数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCopytokd(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
